﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace LibraryManagement.Sach
{
    public partial class uscSach : DevExpress.XtraEditors.XtraUserControl
    {
        readonly SachBus busS = new SachBus();

        public uscSach()
        {
            InitializeComponent();
        }

        private void uscSach_Load(object sender, EventArgs e)
        {
            dgvDauSach.DataSource = busS.layDuLieuDGV();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            frmThem t = new frmThem();
            t.BusDS = busS;
            t.UscDS = this;
            t.ShowDialog();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            busS.Luu();
            MessageBox.Show("Dữ liệu đã được lưu", "Thông Báo", 
                MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dgvDauSach.SelectedCells.Count == 1 || dgvDauSach.SelectedRows.Count == 1)
            {
                DataGridViewRow r = dgvDauSach.Rows[dgvDauSach.CurrentRow.Index];
                busS.Xoa(r.Cells["MaDauSach"].Value.ToString());
                dgvDauSach.DataSource = busS.layDuLieuDGV();
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (dgvDauSach.SelectedCells.Count == 1 || dgvDauSach.SelectedRows.Count == 1)
            {
                DataGridViewRow r = dgvDauSach.Rows[dgvDauSach.CurrentRow.Index];
                DauSachDto dtoDS = new DauSachDto();
                dtoDS.MaDauSach = r.Cells["MaDauSach"].Value.ToString();
                dtoDS.Ten = r.Cells["Ten"].Value.ToString();
                frmSua t = new frmSua();
                t.BusDS = busS;
                t.UscDS = this;
                t.DtoDS = dtoDS;
                t.ShowDialog();
            }
        }
        
        //khi có sự thay đổi về dòng đc chọn của datagridview
        private void dgvDauSach_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvDauSach.SelectedCells.Count == 1 || dgvDauSach.SelectedRows.Count == 1)
            {
                DataGridViewRow r = dgvDauSach.Rows[dgvDauSach.CurrentRow.Index];
                txtMa.Text = r.Cells["MaDauSach"].Value.ToString();
                txtTen.Text = r.Cells["Ten"].Value.ToString();
            }
        }
        
        private void txtTimTen_TextChanged(object sender, EventArgs e)
        {
            dgvDauSach.DataSource = busS.locDuLieuDGV(txtTimTen.Text);
        }

        //khi DoubleClick vào 1 dòng của datagridview
        private void dgvDauSach_DoubleClick(object sender, EventArgs e)
        {
            if (dgvDauSach.SelectedCells.Count == 1 || dgvDauSach.SelectedRows.Count == 1)
            {
                DataGridViewRow r = dgvDauSach.Rows[dgvDauSach.CurrentRow.Index];
                DauSachDto dtoDS = new DauSachDto();
                dtoDS.MaDauSach = r.Cells["MaDauSach"].Value.ToString();
                dtoDS.Ten = r.Cells["Ten"].Value.ToString();
                frmSua t = new frmSua();
                t.BusDS = busS;
                t.UscDS = this;
                t.DtoDS = dtoDS;
                t.ShowDialog();
            }
        }
    }
}
